package com.example.administrator.text;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import data.SharedPreferencesUtils;
import util.AgentIp;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.diaLogView.UploadDialog;

/**
 * 分享
 */
public class ShareActivity extends BaseActivity {
    UploadDialog mUploadDialog;
    private String mRecommend = "";
    private String mUrl = "http://139.199.72.236:8090/NewFiPortal/portal/home.do?recommend=";
    @Bind(R.id.activity_share_frame)   FrameLayout mFrameLoayout;
    @Bind(R.id.activity_share_tv)      TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        HomeUtil.getHemeUtilNew().getStatusBar(this, mFrameLoayout);
        initData();
    }

    /**
     * 返回
     */
    @OnClick(R.id.activity_share_ll)
    void getFinish(){
        finish();
    }

    /**
     * 复制
     */
    @OnClick(R.id.activity_share_copy)
    void copy(){
        ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(mUrl+mRecommend);
    }

    /**
     * 分享
     */
    @OnClick(R.id.activity_share_share)
    void share(){
        if(mUploadDialog == null){
            mUploadDialog = new UploadDialog(this,"连接中");
            mUploadDialog.show();
        }else {
            mUploadDialog.show();
        }
        AgentIp.newInstance(this).setNetWorkstate2(new AgentIp.OnNetworkStateClickListener() {
            @Override
            public void onClick(boolean state) {
                mUploadDialog.dismiss();
                if(state){
                    if(mRecommend.equals("")){
                        initData();
                        getToast("分享码获取失败，请稍后再点击");
                    }else {
                        ShareSDK.initSDK(ShareActivity.this);
                        OnekeyShare oks = new OnekeyShare();
                        //关闭sso授权
                        oks.disableSSOWhenAuthorize();
                        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
                        oks.setTitle("NewFi");
                        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
                        oks.setTitleUrl(mUrl+mRecommend);
                        // text是分享文本，所有平台都需要这个字段
                        oks.setText("NewFi是互联网新型基础运营商，开创全球领先的公共区域网络服务。");
                        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
                        oks.setImageUrl(mUrl+mRecommend);
                        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
                        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
                        // url仅在微信（包括好友和朋友圈）中使用
                        oks.setUrl(mUrl+mRecommend);
                        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                        oks.setComment("NewFiApp");
                        // site是分享此内容的网站名称，仅在QQ空间使用
                        oks.setSite("NewFi");
                        oks.setExecuteUrl(mUrl+mRecommend);
                        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                        oks.setSiteUrl(mUrl+mRecommend);
                        // 启动分享GUI
                        oks.show(ShareActivity.this);
                    }
                }else {
                    getToast("请连接网络后，再分享");
                }
            }
        });
    }

    /**
     * 请求 分享码
     */
    private void initData() {
        mMap = new HashMap<>();
        String id = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mUseridDB, "");
        String token = (String) SharedPreferencesUtils.getParam(ShareActivity.this, HomeUtil.mTokenDB, "");
        mMap.put("userid",id);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.getUrlNew().recomment_outputCode, stringJson,token,id,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(this,stringJson,token,id,Url.getUrlNew().recomment_outputCode,stirngSign,time);
        getLogger().info("initData()---->fwq--" + MyApplication.getApp().getSiteAreaid() + "stringJson--" + stringJson+"...NF_callid..."+map1.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(this,MyApplication.getApp().getFWQString(),1,map1, new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    getLogger().info("initData()---->onSuccess--" + response);
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject.getInt("code") == 1){
                        mRecommend = jsonObject.getString("recommend");
                        mTextView.setText(mUrl+mRecommend);
                        LogUtil.eE("",mUrl+mRecommend);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable error) {
             StringUtil.getStringUtilNew().getExceptionCode(ShareActivity.this, error.toString());
            }
        });
    }

}
